var HistoryModule = function () {
    var editable = function () {

        return {
            init: function () {
            	//格式化金额
            	function fmoney(s, n) {  
            	   if(s == ""|| undefined == s){
            		   return "--";
            	   }
                   n = n > 0 && n <= 20 ? n : 2;  
                   s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";//更改这里n数也可确定要保留的小数位  
                   var l = s.split(".")[0].split("").reverse(),  
                   r = s.split(".")[1];  
                   t = "";  
                   for(i = 0; i < l.length; i++ )  
                   {  
                      t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");  
                   }
                   var str = t.split("").reverse().join("") + "." + r.substring(0,2);
                   return str;//保留2位小数  如果要改动 把substring 最后一位数改动就可  
                }
            	//表格初始化
                var oTable = $('#historyEditable').dataTable({
                    "paginate": false, //翻页功能
                    "autoWidth":false,
                    "displayLength": 10,
                    "info":false,
                    "lengthChange":false,
                    "language": {
                        "zeroRecords":"数据为空",
                        "info" : "从 _START_ 到  _END_ 条，总记录为 _TOTAL_ 条",    
                        "infoEmpty" : "记录数为0",
                    },
                    "columnDefs": [
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "Edit",
						    "targets": [5]
						}            
                    ],
                    "columns": [
                       { "data": "name" ,'sortable': false,"defaultContent": "" },
                       { "data": "amount" ,'sortable': false,"defaultContent": "" },
                       { "data": "employeeName" ,'sortable': false,"defaultContent": "" },
                       { "data": "time" ,'sortable': false,"defaultContent": "" },
                       { "data": "state" ,'sortable': false,"defaultContent": "" },
                   ],
                   "filter" : false,
                   "processing": false,
                   "serverSide": true,
                   "ajaxSource": ctx + "/assetAllocation/queryHistory",
                   "serverMethod": "post",
                   "fnRowCallback" : function(nRow, aData, displayIndex) {
                	   nRow.children[0].id = aData.id;
                	   var state = "";
                	   switch(aData.state){
	                	   case (2):
	                		   state = "审批中";
	                           break;
	                       case (3):
	                    	   state = "终审中";
                           	   break;
	                       case (4):
	                    	   state = "过终审";
                           	   break;
	                       case (5):
	                    	   state = "已投资";
                           	   break;
	                       default:
	                    	   state = "审批中";
                	   }
                	   
                	   var operation = "";
                	   operation = '<a class="detail fa fa-bars fa-lg pull-left" title="详情" href="javascript:;"></a>';
                	   $('td:eq(1)', nRow).html(fmoney(aData.amount));	
                	   $('td:eq(4)', nRow).html(state);	
                	   $('td:eq(5)', nRow).html(operation);
                       return nRow;
                   },
                   "fnServerData" : function(ajaxSource, dataSet, fnCallback) {
	                   var json = "";
	                   var s;
	                   for (var i = 0; i < dataSet.length; i++) {
	                       s = dataSet[i];
	                   	   if (s.name == 'sEcho') {
	                   		   json = json + '"echo":' + s.value;
		                   } else if (s.name == 'iDisplayStart') {
		                	   json = json + ',"start":' + s.value;
		                   } else if (s.name == 'iDisplayLength') {
		                	   json = json + ',"pageSize":' + s.value;
		                   }
	                   }
	                   var obj = {};
	                   var productId = $("#productId").val();
	                   obj.id = productId;
	                   var json = JSON.stringify(obj); 
                       $.ajax({
                       	"contentType":"application/json; charset=UTF-8",
                           "dataType" : 'json',
                           "type" : "POST",
                           "url" : ajaxSource,
                           "data" : json,
                           "success" : fnCallback
                       });
                   },
                });
                
                // 操作开始
                
                // 详情
                $('#historyEditable').on("click", "a.detail", (function (e) {
                    e.preventDefault();
                    var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                    window.location = ctx + '/assetAllocation/assetDetail/' + id + '';
                }));
            }

        };

    }();
    
    return {
        init: function () {
        	editable.init();
        }
    };
}();